close all;
n = 200;
idx = ceil(rand()*(n - 1));
% idx = 43


%% RPCA result
load hall_initial;
N = XO;

% noisy frame i
figure;
Ni = N(:, idx);
Ni = reshape(Ni, 144, 176);
imshow(Ni, []);

% clean frame i
figure;
Li = L(:, idx);
Li = reshape(Li, 144, 176);
imshow(Li, []);

% sparse noise i
figure;
Si = S(:, idx);
Si = reshape(Si, 144, 176);
imshow(abs(Si), []);

% gaussian noise i
figure;
Gi = G(:, idx);
Gi = reshape(Gi, 144, 176);
imshow(abs(Gi),[]);

%% MoG result with warm start
load hall_warm_on_ref;
% noisy frame i
N = XO;
X = A*B';
[d,n] = size(X);
k = numel(model.weight);

figure;
Ni = N(:, idx);
Ni = reshape(Ni, 144, 176);
imshow(Ni, []);

% clean frame i
figure;
Xi = X(:, idx);
Xi_on = reshape(Xi,144, 176);
imshow(Xi_on, []);

MoGi = Ni - Xi_on;
Ri = R(d*(n-1)+1:d*n,:);
[~,index] = sort(Ri,2,'descend');
index = index(:,1);
[~,sigmaorder] = sort(model.Sigma);
gi_on = model.Sigma(sigmaorder);
for j = 1:k
    gass = MoGi;
    gass((index ~= j)) = 0;
    Gi_on(:,:,find(sigmaorder==j)) = reshape(gass,144, 176);
subplot(1,k,j), imshow(abs(gass),[])
    str=sprintf('sigma = %d', 255*sqrt(model.Sigma(j)));
    title(str);
end
%% MoG result(reference) with warm start
load('hall_warm_off_ref')
% noisy frame i
N = XO;
X = A*B';
[d,n] = size(X);
k = numel(model.weight);

figure;
Ni = N(:, idx);
Ni = reshape(Ni, 144, 176);
imshow(Ni, []);

% clean frame i
figure;
Xi = X(:, idx);
Xi_off = reshape(Xi,144, 176);
imshow(Xi_off, []);

MoGi = Ni - Xi_off;
Ri = R(d*(n-1)+1:d*n,:);
[~,index] = sort(Ri,2,'descend');
index = index(:,1);
[~,sigmaorder] = sort(model.Sigma);
gi_off = model.Sigma(sigmaorder);
for j = 1:k
    gass = MoGi;
    gass((index ~= j)) = 0;
    Gi_off(:,:,find(sigmaorder==j)) = reshape(gass,144, 176);
%     str=sprintf('sigma = %d', 255*sqrt(model.Sigma(j)));
%     title(str);
end